'use strict';

module.exports = (app) => {
  class CarouselGroupService extends app.Service {
    async create() {
      const { app, ctx } = this;
      const { CarouselGrop } = ctx.model.Tables.Shop;
      const groupData = ctx.request.body;

      try {
        /** 通过token验证后的信息获取用户的项目ID */
        groupData.project_id = ctx.adminInfo.project_id;
        const res = await CarouselGrop.create(groupData);
        /** 去除掉时间戳 */
        res.dataValues.createdAt = undefined;
        res.dataValues.updatedAt = undefined;
        return res;
      } catch (error) {
        return error.message ? error.message : error;
      }
    }

    async delete() {
      const { app, ctx } = this;
      const { CarouselGrop, Carousel } = app.model.Tables.Shop;
      const data = ctx.request.body;
      //获取token
      const token = ctx.get('authorization');

      await CarouselGrop.destroy({
        where: {
          id: data.id,
        },
      });

      await Carousel.destroy({
        where: {
          site_id: data.id,
        },
      });
      return '删除成功';
    }

    async get() {
      const { app, ctx } = this;
      const { CarouselGrop } = ctx.model.Tables.Shop;

      try {
        const project_id = ctx.adminInfo.project_id;
        const res = await CarouselGrop.findAll({
          where: {
            project_id,
          },
        });

        return res;
      } catch (error) {
        return error.message ? error.message : error;
      }
    }

    async show(id) {
      const { ctx } = this;
      const { Carousel, CarouselGrop, Photo } = ctx.model.Tables.Shop;

      const res = await CarouselGrop.findOne({
        where: {
          id,
        },
        include: [
          {
            model: Carousel,
            include: [
              {
                model: Photo,
              },
            ],
          },
        ],
      });

      return res && res.dataValues.s_carousels;
    }
  }
  return CarouselGroupService;
};
